<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Email;
use app\common\model\User;
use app\admin\model\Code;

// 引入Code模型

class Userapi extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    // 发送恢复密码邮件
// 发送恢复密码邮件或修改密码
    public function recover()
    {
        // 获取 POST 请求参数
        $email = $this->request->post('email');
        $code = $this->request->post('code');
        $password = $this->request->post('password');

        // 查询验证码是否正确
        $codeModel = Code::where('email', $email)->where('code', $code)->find();
        if (!$codeModel) {
            return json([
                'code' => 201,
                'message' => '验证码错误，请检查！'
            ]);
        }

        // 使用User模型根据email修改对应的password，或者发送邮件获取验证码
        if ($password !== null) {
            // 修改密码
            $user = User::where('email', $email)->find();
            if (!$user) {
                return json([
                    'code' => 201,
                    'message' => '邮箱不存在，密码修改失败！'
                ]);
            }
            $user->password = $password;
            $result = $user->save();

            // 返回结果或提示信息
            if ($result !== false) {
                // 删除验证码记录
                $codeModel->delete();

                return json([
                    'code' => 200,
                    'message' => '密码修改成功！'
                ]);
            } else {
                return json([
                    'code' => 201,
                    'message' => '密码修改失败，请稍后再试！'
                ]);
            }
        } else {
            // 发送邮件获取验证码
            $domain = $this->request->domain();
            $link = $domain . '/index/user/recovers.html?email=' . $email . '&code=' . $code;

            // 发送邮件给指定邮箱
            $text = '请点击以下链接修改密码：' . $link;

            // 获取邮件对象
            $emailObj = new Email;
            $result = $emailObj
                // 接收邮箱地址
                ->to($email)
                // 邮件主题
                ->subject(__("修改密码"))
                // 邮件正文内容
                ->message($text)
                // 发送
                ->send();

            // 返回结果或提示信息
            if ($result !== false) {
                return json([
                    'code' => 200,
                    'message' => '密码修改链接已发送，请查收！'
                ]);
            } else {
                return json([
                    'code' => 201,
                    'message' => '邮件发送失败，请稍后再试！'
                ]);
            }
        }
    }

    // 注册账号
    public function register()
    {
        // 获取 POST 请求参数
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $email = $this->request->post('email');

        // 在这里实现注册逻辑，比如将用户信息保存到数据库

        // 查询账号或邮箱是否已存在
        $user = User::where('username', $username)->whereOr('email', $email)->find();
        if ($user) {
            return json([
                'code' => 201,
                'message' => '账号或邮箱已存在，注册失败！'
            ]);
        }

        // 创建User模型对象
        $user = new User;
        $user->username = $username;
        $user->password = $password;
        $user->email = $email;
        $result = $user->save();

        // 返回结果或提示信息
        if ($result) {
            return json([
                'code' => 200,
                'message' => '注册成功！'
            ]);
        } else {
            return json([
                'code' => 201,
                'message' => '注册失败，请稍后再试！'
            ]);
        }
    }

    // 修改邮箱和密码,仅判断username是否存在和email是否重复
    public function update()
    {
        // 获取 POST 请求参数
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $email = $this->request->post('email');

        // 查询账号是否存在
        $user = User::where('username', $username)->find();
        if (!$user) {
            return json([
                'code' => 201,
                'message' => '账号不存在，修改失败！'
            ]);
        }
        $isemail = User::where('email', $email)->find();
        if ($isemail) {
            return json([
                'code' => 201,
                'message' => '邮箱已存在，修改失败！'
            ]);
        }


        // 修改邮箱和密码
        $user->password = $password;
        $user->email = $email;
        $result = $user->save();

        // 返回结果或提示信息
        if ($result !== false) {
            return json([
                'code' => 200,
                'message' => '恭喜你,修改成功！'
            ]);
        } else {
            return json([
                'code' => 201,
                'message' => '修改失败，请稍后再试！'
            ]);
        }
    }

    // 发送随机6位数到指定邮箱
    public function send()
    {
        // 获取 POST 请求参数
        $email = $this->request->post('email');

        // 生成随机6位数
        $randomNumber = mt_rand(100000, 999999);

        // 发送邮件给指定邮箱
        $text = '您的验证码是：' . $randomNumber;

        // 获取邮件对象
        $emailObj = new Email;
        $result = $emailObj
            // 接收邮箱地址
            ->to($email)
            // 邮件主题
            ->subject(__("验证码"))
            // 邮件正文内容
            ->message($text)
            // 发送
            ->send();

        // 插入数据到Code模型
        if ($result !== false) {
            $code = new Code;
            $code->email = $email;
            $code->code = $randomNumber;
            $code->save();

            return json([
                'code' => 200,
                'message' => '验证码已发送，请查收！',
                'random_number' => $randomNumber
            ]);
        } else {
            return json([
                'code' => 201,
                'message' => '邮件发送失败，请稍后再试！'
            ]);
        }
    }

}
